
(function() {
    var themeIndexedDB = function() {};
    themeIndexedDB.prototype = {
        db: null,
		dbName:"theme_edit",
        render: function(param, callback) {
            if (!window.indexedDB) {
                return
            }
            var self = this;
            var customerData = [param && param.initConfig || {
                name: "theme",
				id:"init"
            }];
            var request = indexedDB.open(this.dbName, 1);
            request.onsuccess = function(event) {
                self.db = event.target.result;
                if (callback) {
                    callback();
                }
            }
            request.onupgradeneeded = function(event) {
                self.db = event.target.result;

                // Create an objectStore to hold information about our customers. We're
                // going to use "ssn" as our key path because it's guaranteed to be
                // unique.
                var objectStore = self.db.createObjectStore(param.config, {
                    keyPath: "id"
                });



                // Use transaction oncomplete to make sure the objectStore creation is 
                // finished before adding data into it.
                objectStore.transaction.oncomplete = function(event) {
                    // Store values in the newly created objectStore.
                    var customerObjectStore = self.db.transaction(param.config, "readwrite").objectStore(param.config);
                    for (var i in customerData) {
                        customerObjectStore.add(customerData[i]);
                    }
                }
            };
        },
		closeDB:function(){
			var db=themeDB.db;
			if(db){
				db.close();
			}
            setTimeout(function(){
                indexedDB.deleteDatabase(this.dbName);
            }, 0);
		},
        getItem: function(param, callback) {
			var self=this;
            this.render(param, function() {
                var config = param.config;
                var objectStore = self.db.transaction([param.config], "readwrite").objectStore(param.config);
                var request = objectStore.get(param.id);
                request.onerror = function(event) {
                    // Handle errors!
                };
                request.onsuccess = function(event) {
                    // Get the old value that we want to update
                    var data = request.result;
                    if (callback) {
                        callback(data);
                    }
                };
            });
        },
        setItem: function(param, callback) {
			var self=this;
            var config = param.config;
			this.render(param,function(){
				var objectStore = self.db.transaction([param.config], "readwrite").objectStore(param.config);
				var request = objectStore.get(param.config);
				request.onerror = function(event) {
					// Handle errors!
				};
				request.onsuccess = function(event) {
					// Get the old value that we want to update
					//var data = request.result;

					// Put this updated object back into the database.
					var requestUpdate = objectStore.put(param.initConfig);
					requestUpdate.onerror = function(event) {
						// Success - the data is updated!
						$('#myModal').modal({
							backdrop: "static"
						}).find(".modal-body").html('<p class="tip"><img src="http://designer.res.meizu.com/resources/theme/images/info.png">icons文件目录过多！</p>');
						$('#myModal').find(".modal-footer").show();
						// Do something with the error
					};
					requestUpdate.onsuccess = function(event) {
						// Success - the data is updated!
						if (callback) {
							callback()
						}
					};
				};
			});
        }
    }
    $(function() {
        window.themeDB = new themeIndexedDB();
    });

})()